package com.bbex.controller;

import com.bbex.pojo.GlobalMessageResponseVo;
import com.bbex.util.FileTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import sun.misc.BASE64Decoder;
import java.util.UUID;

@RestController
@RequestMapping("/bbuni")
public class UploadImage {

    private static String uploadFolder;

    @Value("${file.uploadFolder}")
    public void setEwmUrl(String ewmurl) { uploadFolder = ewmurl;}

    @PostMapping("/ewmImage")
    public Mono<GlobalMessageResponseVo> fileUpload(String imgStr) {
        imgStr = "iVBORw0KGgoAAAANSUhEUgAAAJQAAACUCAYAAAB1PADUAAAInElEQVR4Xu3d0W7jOgyE4fb9H7oLnLs4i3yYQ8rpttNbyhQ5/EXJjuF+fn19fX30rwosKfBZoJaUrJv/FChQBWFVgQK1KmedFagysKpAgVqVs84KVBlYVaBArcpZZwWqDKwqUKBW5ayzAlUGVhUoUKty1lmBKgOrChSoVTnrrECVgVUFCtSqnHU2Burz8/NWFfX61jWe63jZr8mk409ff1ps6av5CxTeLyxQQujRXqAK1AMR7VCXBaSOIvvpLSst2Hc7UqhfrXeoVDAGeDmjTf2nBUrPYAJW86fzST/ZFa+uf1qA03fKtwPa7hDyJ8HSAkuPAgXFJaAKJvu2fxX0Gk+BUoUOH8qnW5I6ytR/gboAsH2kOL3l3V3AbD3lo6cdSx0wXVCn9U0VOn4oP51w6j8VSABoS1Z86rhT/9P4U70KVKhYO9RrwQpUgfreDza1glU/XT+1p/PrTCN/2nJ0fbe8y08ZOkNI8FRQAacCpmca+VN+uj7NX/6kj/KX/x+35UkwCSJB0wVSoKT4xf7uFXR6/m55GRA/rkNl6X98bHek7Q55esHIf6pngQpfX5HABUpLFAqK8PTMoYLIroKnZ5rt+BWf8pN927/8PR0JfvtPL1pPBSpD6viWl4XzPDrtgNuATONXR9zOL41X88f+TneoNCDdVanlF6hM8QKFB6cFqkC9VEArS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VwVKgQEmh2iMFClQkVwdLgQIlhWqPFChQkVwdLAUKlBSqPVKgQEVydbAUKFBSqPZIgQIVydXBUuAPCeie+dvYIzoAAAAASUVORK5CYII=";
        if (imgStr == null) { //图像数据为空
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try{
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for(int i=0;i<b.length;++i){
                if(b[i]<0){//调整异常数据
                    b[i]+=256;
                }
            }
            String imgName = UUID.randomUUID()+".png";
            FileTool.uploadFiles(b,uploadFolder,imgName);
            System.out.println(imgName);
            return Mono.just(GlobalMessageResponseVo.newSuccessInstance(imgName));
        }catch (Exception e){
            return null;
        }
    }
}
